package software.design.pattern.creative.method;

/**
 * @author soento
 */
public class AnimalFarmTest {
    public static void main(String[] args) {
        try {
            Animal a;
            AnimalFarm af = new HorseFarm();
            a = af.newAnimal();
            a.show();
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        try {
            Animal a;
            AnimalFarm af = new CattleFarm();
            a = af.newAnimal();
            a.show();
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }
}

/**
 * 抽象产品：动物类
 */
interface Animal {
    void show();
}

/**
 * 具体产品：马类
 */
class Horse implements Animal {

    @Override
    public void show() {
        System.out.println("动物：马");
    }
}

/**
 * 具体产品：牛类
 */
class Cattle implements Animal {

    @Override
    public void show() {
        System.out.println("动物：牛");
    }
}

/**
 * 抽象工厂：畜牧场
 */
interface AnimalFarm {
    Animal newAnimal();
}

/**
 * 具体工厂：养马场
 */
class HorseFarm implements AnimalFarm {
    @Override
    public Animal newAnimal() {
        System.out.println("新马出生！");
        return new Horse();
    }
}

/**
 * 具体工厂：养牛场
 */
class CattleFarm implements AnimalFarm {
    @Override
    public Animal newAnimal() {
        System.out.println("新牛出生！");
        return new Cattle();
    }
}
